package bim.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import bim.dao.UserDao;

/**
 * <p>
 * Class : keter.web.security.KeterUserDetailsService
 * <p>
 * Descdription: 返回用户详细信息
 * 
 * @author gulixing@msn.com
 * @version 1.0.0
 */
@Service("UserDetailsService")
@Transactional
public class UserDetailsServiceImpl implements UserDetailsService {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(UserDetailsServiceImpl.class);
	
	@Autowired
	private UserDao dao;

	/**
	 * <p>
	 * Method ：loadUserByUsername
	 * <p>
	 * Description : 从数据库中读取用户，由SS判断用户密码是否正确
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
	 * @author gulixing@msn.com
	 * @version 1.0.0
	 */
	@Override
	public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException, DataAccessException {
			UserAdapter user = new UserAdapter(dao.findByName(name), dao);
			if (user.getAuthorities().isEmpty()) {
				throw new AuthenticationCredentialsNotFoundException("用户身份验证失败:错误的用户名或密码！");
			}
			return user;
	}

	public void setUserDao(UserDao dao) {
		this.dao = dao;
	}
}
